//
//  XKLessenceCell.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/5/4.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLessenceCell.h"
#import "XKLEessenceItem.h"
#import <UIImageView+WebCache.h>
#import "XKLPictureView.h"
#import "XKLVideoAndVoiceVIew.h"
#import "XKLTopCommetItem.h"
#import "XKLUserItem.h"

@interface XKLessenceCell()
/**名字*/
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
/**创建时间*/
@property (weak, nonatomic) IBOutlet UILabel *createTimer;
/**头像*/
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
/**收藏*/
@property (weak, nonatomic) IBOutlet UIButton *colletionBut;
/**留言*/
@property (weak, nonatomic) IBOutlet UIButton *messgeBut;
/**踩*/
@property (weak, nonatomic) IBOutlet UIButton *caiBut;
/**转发*/
@property (weak, nonatomic) IBOutlet UIButton *zhuanFaBut;
/**顶*/
@property (weak, nonatomic) IBOutlet UIButton *dingBut;
/**新浪是否加v*/
@property (weak, nonatomic) IBOutlet UIImageView *sina_V;
/**正文*/
@property (weak, nonatomic) IBOutlet UILabel *textLable;
/**图片*/
@property (weak,nonatomic) XKLPictureView *pictureView;
/**视频和音频*/
@property (weak,nonatomic) XKLVideoAndVoiceVIew *videoAndVoice;
/**评论的view*/
@property (weak, nonatomic) IBOutlet UIView *topCommentView;
/**评论的label*/
@property (weak, nonatomic) IBOutlet UILabel *top_commentLable;
@end

@implementation XKLessenceCell

-(XKLPictureView *)pictureView{
    if (_pictureView == nil) {
        XKLPictureView *imageView=[XKLPictureView picture];
        _pictureView =imageView;
        [self.contentView addSubview:imageView];
    }
    return _pictureView;
}

-(XKLVideoAndVoiceVIew *)videoAndVoice{
    if (_videoAndVoice == nil) {
        XKLVideoAndVoiceVIew *video=[XKLVideoAndVoiceVIew videoAndVoice];
        [self.contentView addSubview:video];
        _videoAndVoice = video;
    }
    return _videoAndVoice;
}

- (void)awakeFromNib {
    // Initialization code
}


-(void)setItem:(XKLEessenceItem *)item{
    _item=item;
    
    (item.isSinaV) ? (self.sina_V.hidden=NO) : (self.sina_V.hidden=YES);
    
    self.nameLable.text=item.name;
    self.createTimer.text=item.created_at;
    
    //是否显示热评
    if (item.top_cmt) {
        self.topCommentView.hidden = NO;
        self.top_commentLable.text = [NSString stringWithFormat:@"%@:%@",item.top_cmt.user.username,item.top_cmt.content];
    }else{
        self.topCommentView.hidden = YES;
    }
    
    //图片的显示
    if (item.type == kXKLTopicTypePicture) {
        self.pictureView.item = item;
        self.pictureView.frame = item.frame;
        //循环利用使用要隐藏
        self.pictureView.hidden = NO;
        self.videoAndVoice.hidden = YES;
    }else if (item.type == kXKLTopicTypeVoice || item.type == kXKLTopicTypeVideo) {
        self.videoAndVoice.item = item;
        self.videoAndVoice.frame = item.frame;
        //循环利用使用要隐藏
        self.videoAndVoice.hidden =NO;
        self.pictureView.hidden = YES;
    } else {
        self.videoAndVoice.hidden = YES;
        self.pictureView.hidden = YES;
    }
    
    if (item.iconImage==nil) {
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:item.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            //开启上下文
            UIGraphicsBeginImageContextWithOptions(self.iconImageView.frame.size, NO, [UIScreen mainScreen].scale);
            //剪切路径
            UIBezierPath *bezierPath=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.iconImageView.width, self.iconImageView.height)];
            
            //剪切
            [bezierPath addClip];
            [image drawInRect:CGRectMake(0, 0, self.iconImageView.width, self.iconImageView.height)];
            
            //获取图片
            self.iconImageView.image=UIGraphicsGetImageFromCurrentImageContext();
            
            item.iconImage=self.iconImageView.image;
            //关闭上下文
            UIGraphicsEndImageContext();
        }];
    }else{
        self.iconImageView.image=item.iconImage;
    }
    self.textLable.text = item.text;
    [self setButTitleWithBut:self.caiBut count:item.cai];
    [self setButTitleWithBut:self.dingBut count:item.ding];
    [self setButTitleWithBut:self.zhuanFaBut count:item.repost];
    [self setButTitleWithBut:self.messgeBut count:item.comment];
    
    
}
//设置按钮的文字
-(void)setButTitleWithBut:(UIButton *)but count:(NSInteger)count{
    NSString *countStr;
    
    if (count>10000) {
        countStr=[NSString stringWithFormat:@"%.1lf万",count/10000.0];
    }else{
        countStr=[NSString stringWithFormat:@"%ld",count];
    }

    [but setTitle:countStr forState:UIControlStateNormal];
}

//收藏和举报按钮
- (IBAction)ShouCanButClick:(id)sender {
    
    UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"举报",@"收藏", nil];
    
    //添加到窗口或者根控制器的上面
    [sheet showInView:[UIApplication sharedApplication].keyWindow.rootViewController.view];
    
}


//重写frame的set方法
-(void)setFrame:(CGRect)frame{
    
    //重新布置cell的位置
    frame.origin.x = XKLAllCellMargin;
    frame.size.width = ScreenW - 2 * frame.origin.x;
    frame.size.height = self.item.cellHeight - XKLAllCellMargin;
    [super setFrame:frame];
}

@end
